define([
    "app"
], function (app, moment) {
    app.controller("indexController", [
        "$scope",
        "indexDataService",
        "$uibModal",
        "dateService",
        "$state",
        function ($scope, indexDataService, $uibModal, dateService,$state) {

            indexDataService
                .getStatistic()
                .then(
                    function success(resp) {
                        $scope.statistic = resp.data;

                        // 当前日期
                        $scope.currentDate = new Date();

                        // 打开发布申请的对话框
                        $scope.openDialog = function () {
                            $uibModal.open({
                                animation: true,
                                size: "sm",
                                backdrop: "static",
                                controller: "createNewApplicationRoundDialogController",
                                templateUrl: "create-new-application-round-dialog.html"
                            });
                        };

                        $scope.compareDate = function (d1, d2) {
                            return dateService.compare(d1, d2,"YYYY/MM/DD");
                        };

                        // 关闭当前申请
                        $scope.closeCurrentApplication = function () {
                            if(!confirm("确定要关闭当前申请吗?")) {
                                return;
                            }

                            indexDataService
                                .closeCurrentApplicationRound({
                                    termId: $scope.statistic.applicationRound.termId
                                })
                                .then(
                                    function success(resp) {
                                        alert("发布成功!");
                                        // 刷新页面
                                        $state.go($state.current, {}, {reload: true});
                                    },
                                    function error(resp) {
                                        alert("似乎出了点问题!!!∑(ﾟДﾟノ)ノ");
                                        // 刷新页面
                                        $state.go($state.current, {}, {reload: true});
                                    }
                                )
                        }

                    },
                    function error(resp) {

                    }
                );
        }
    ]);
});